<%@page import="java.math.BigDecimal"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ include file="/classes/DBHelper.class.jsp" %>
<%@ include file="/classes/CommonHelper.class.jsp" %>
<%@ include file="/classes/GUID.class.jsp" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="com.ucap.system.db.*,oracle.jdbc.driver.OracleDriver"%>
<%@page import="java.io.*,java.util.*,java.sql.*,org.apache.commons.io.FileUtils,org.apache.commons.logging.Log,org.apache.commons.logging.LogFactory" %>
<%@page import="com.ucap.base.model.*,com.ucap.migration.*,com.ucap.utils.*" %>
<%@page import="java.util.Date,java.text.SimpleDateFormat,com.ucap.migration.manuscript.*" %>
<%@page import="com.ucap.migration.resource.*,com.ucap.base.model.wcm.Manuscript" %>
<%@page import="com.ucap.migration.helper.LoadObjectSupport"%>
<%@page import="javax.servlet.ServletResponse,javax.servlet.ServletRequest,javax.servlet.ServletException,javax.servlet.http.HttpServlet,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse"%>

<%
//获取参数
String website_code = getParameterAndSetAttribute(request,"website_code");//站点ID
if(!nullOrEmpty(website_code)){
	website_code = website_code.trim();
}
// String channelid = getParameterAndSetAttribute(request,"channelid");
// String channel_id = getParameterAndSetAttribute(request,"channel_id");
// String currPage = getParameterAndSetAttribute(request,"currPage");
// if(nullOrEmpty(currPage)){
// 	currPage = "1";
// 	pageContext.setAttribute("currPage",currPage);
// }
// String pageSize = getParameterAndSetAttribute(request,"pageSize");
// if(nullOrEmpty(pageSize)){
// 	pageSize = "200";
// 	pageContext.setAttribute("pageSize",pageSize);
// }
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>数据迁移</title>
<style type="text/css">
label {
	width: 150px;
	display: inline-block;
	text-align: right;
}
</style>
</head>
<body>
<form action="importDateWscwhXygs.jsp?r=<%=new Date().getTime() %>" method="post">
<table>
	<tr>
		<td><label>目标站点</label><input type="text" name="website_code" value="${website_code }" /></td>
	</tr>
	<tr>
		<td colspan="2" align="center"><input type="submit" value="提交" /> </td>
	</tr>
</table>
</form>
<%
if(!nullOrEmpty(website_code)){
	
// 	out.print(classid+"");
// 	out.print(channel_id);
// 	out.print(currPage);
// 	out.print(pageSize);
	int count1 = 0;//计数器
	int count2 = 0;//计数器
	int count3 = 0;//计数器
	int count4 = 0;//计数器
	//检索所有东圩社区的稿件
	//东圩社区稿件
	String sql = " select t1.title, t1.sub_title, t1.published_time,t1.created_time, t4.code_name website_code, t2.code_name, t1.url, t2.channel_name c_name, t3.channel_name pc_name ,t1.type "+
				"   from cms_manuscript t1 left join  cms_channel t2 on t1.channel_id = t2.channel_id left join cms_channel t3 on t2.parent_id = t3.channel_id left join cms_website t4 on t1.website_id = t4.website_id"+
				" where  t1.website_id = 'c909de1a750e45beb6061e31c8d1591e' "+
				"   and t1.status = 4 "+
				"   and title <> '站内搜索' "+
				"   and t2.code_name = 'xygs'";
	
	List<Map<String,Object>> listWscwh = queryForList(sql);
	
	//获取站点下所有栏目
	String sqlChannel = //"select t1.channel_id,t1.channel_name ,t2.channel_id pc_id ,t2.channel_name pc_name from cms_channel t1,cms_channel t2 where t1.parent_id = t2.channel_id and t1.website_id = ?";
		"select t.channel_id from cms_channel t where t.website_id in "+
	    "   ( select t.website_id from cms_website t where t.parent_website_id = ? "+
	    "       and t.status <> 4 "+
	    "       and t.parent_website_id not in ('dc22ec4fedf84e8684732dd0d7f6b468','43e24ce386a84697bf3ad65d30e3f295','95cbf27e174e44d98b11d0b954b8969d', 'fac38b56a7ea4420909ddaa742100653','b4a49ef93376453798534b43ad04826e','c04c2544a9484eb4a2ab0805d1bcb460')) "+
	    " and t.channel_name = '信用公示' ";
	
	List<Map<String,Object>> listChannel = queryForList(sqlChannel,new Object[]{website_code});
// 	listChannel = listChannel.subList(1,2);
	for(Map<String,Object> m : listChannel){
		for(int i=0;i<listWscwh.size();i++){
			Map<String,Object> manuscriptMap = listWscwh.get(i);
			String title = (String)manuscriptMap.get("title");
			String code_name = (String)manuscriptMap.get("code_name");
			String url = (String)manuscriptMap.get("url");
			String type = (String)manuscriptMap.get("type");
			String content = getContent("/usr/local/UCMSServer/tomcat/webapps/dwsq/"+code_name+"/"+url,type);//(String)manuscriptMap.get("content");
			Manuscript manuscript = new Manuscript();
			boolean result = false;
			
			manuscript.setManuscriptId(getGUID());
			manuscript.setChannelId((String)m.get("channel_id"));
			manuscript.setTitle(title);// 去掉title空格
			manuscript.setType("0");// 这里的type为1时可能出现稿件生成静态页正文出不来，显示为图片
			manuscript.setStatus("1");// 这里的type直接为3（已签发）时可能出现稿件静态页不生成。
			manuscript.setKeyword("");
			Date date = new Date(Long.valueOf(manuscriptMap.get("created_time").toString()));
			Date date2 = new Date(Long.valueOf(manuscriptMap.get("published_time").toString()));
			
			long createTime = TimeUtil.dateStrToGMTLong(TimeUtil.dateToString("yyyy-MM-dd hh:mm:ss", date),"GMT+8:00");
			long pubTime = TimeUtil.dateStrToGMTLong(TimeUtil.dateToString("yyyy-MM-dd hh:mm:ss",date2),"GMT+8:00");
			long ctttt = TimeUtil.dateStrToGMTLong(TimeUtil.dateToString("yyyy-MM-dd hh:mm:ss",date),"GMT+8:00");				

			manuscript.setCreatedTime(ctttt);
			manuscript.setPublishedTime(pubTime);
			manuscript.setIsDisplayContent("0");
			manuscript.setIsSticky("0");
			manuscript.setCreatorId("4510837f06d84acb8c373dba782b52bc");	//张博
			
			manuscript.setIsModifiedSeqNum("0");
			manuscript.setSortedTime(0l);
			manuscript.setIsEncrypted("0");
			manuscript.setIsPageBreak("0");
			manuscript.setSeqNum(date2.getTime());
			manuscript.setModifiedTime(System.currentTimeMillis());
			manuscript.setMemo("");
			SimpleDateFormat sff = new SimpleDateFormat("yyyyMMdd");
			manuscript.setUrl(sff.format(new Date(createTime)));

			if (!manuscript.getUrl().endsWith(".shtml")) {// 新增稿件
				manuscript.setUrl(manuscript.getUrl() + "/"
				+ manuscript.getManuscriptId() + ".shtml");// 生成稿件URL
			}
			
			try{
				LoadObjectSupport dd = new LoadObjectSupport();
			}catch (Exception e){
			}
			
			com.ucap.migration.manuscript.ManuscriptService manuscriptService = new com.ucap.migration.manuscript.ManuscriptService();
			com.ucap.migration.resource.ResourceTextHandle resourceTextHandle = new ResourceTextHandle();
			String contentPath ="/UCMSServer/webapps/website-webapp/WEB-INF/classes/conf/content";

			try {   
				resourceTextHandle.saveText(manuscript, content);
				result = manuscriptService.saveManuscript(manuscript);
				count1++;
				if(result){
					count2++;
				}else{
					count3++;
				}
			} catch (Exception e) {
				out.println(e.getMessage()+"<br/>");
			//	out.println("channel_id:"+m.get("channel_id")+";channel_id:"+manuscriptMap+";导入失败<br/>");
				count4++;
			}
		}
	}
	out.print("总共导入："+count1+"条数据<br/>");
	out.print("成功导入："+count2+"条数据<br/>");
	out.print("失败导入："+count3+"条数据<br/>");
	out.print("出现异常："+count4+"条数据<br/>");
}

%>
</body>
</html>

<%!
/**
*获取稿件内容
*/
public String getContent(String path,String type) {
		StringBuffer sb = new StringBuffer();
		String content = "";
		 try {
			 //java.net.URL url = new java.net.URL("http://www.cnsn.gov.cn"+path);
			 File file = new File(path);
			 InputStream inputStream = new FileInputStream(file);
			 BufferedReader in = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
			 String line;
			 while ((line = in.readLine()) != null) {
				 sb.append(line);
			 }
			 content = sb.toString();
			 if(type.equals("0")){
				 content = content.substring(content.indexOf("<UCAPCONTENT>")+13, content.indexOf("</UCAPCONTENT>"));
			 }
			 
				// content = new String(content.getBytes(),);
			 System.out.print(content.indexOf("<td class=\"xxgk_content_info\" align=\"center\" valign=\"middle\">"));
			 System.out.print(content.indexOf("<div id=\"zoomtitle\"  class=\"xxgk_content_title\">"));
			 if(type.equals("1")){
				 content = content.substring(content.indexOf("<td class=\"xxgk_content_info\" align=\"center\" valign=\"middle\">"), content.indexOf("<div id=\"zoomtitle\"  class=\"xxgk_content_title\">"))+content.substring(content.indexOf("<UCAPCONTENT>")+13, content.indexOf("</UCAPCONTENT>"));
			 }
			 System.out.println(content);
			 in.close();
		 } catch (Exception e) { // Report any errors that arise
			 sb.append(e.toString());
			 System.err.println(e);
			 System.err.println("Usage:   java   HttpClient   <URL>   [<filename>]");
		 }
		 return content;
	}
%>